package cdu.zch.healthcare;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class ProfileActivity extends AppCompatActivity {

    private ImageView imageViewAvatar;
    private TextView textViewUsername;
    private TextView textViewPoints;
    private TextView textViewAppointment1;
    private TextView textViewMedication1;
    private Database dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        // 底部导航栏点击事件处理
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_home:
                        //
                        startActivity(new Intent(ProfileActivity.this, HomeActivity.class));
                        return true;
                    case R.id.nav_profile:
                        // 处理个人菜单项的点击事件
                        return true;
                    default:
                        return false;
                }
            }
        });
        // Initialize views
        imageViewAvatar = findViewById(R.id.imageViewAvatar);
        textViewUsername = findViewById(R.id.textViewUsername);
        textViewPoints = findViewById(R.id.textViewPoints);
        textViewAppointment1 = findViewById(R.id.textViewAppointment);
        textViewMedication1 = findViewById(R.id.textViewMedication);

        SharedPreferences sharedPreferences = getSharedPreferences("shared_prefs", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        if (!username.isEmpty()) {
            // 从数据库获取用户数据并更新界面
            loadUserData(username);
        } else {
            Toast.makeText(this, "用户名未找到，请重新登录", Toast.LENGTH_SHORT).show();
            // 如果没有找到用户名，可以跳转回登录界面
            startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
            finish();
        }

        // Set click listeners for appointment and medication items
        textViewAppointment1.setOnClickListener(v -> {
            // Handle click to view appointment details
            Intent intent = new Intent(ProfileActivity.this, AppointmentRecordActivity.class);
            intent.putExtra("recordType", "Appointment");
            startActivity(intent);
        });

        textViewMedication1.setOnClickListener(v -> {
            // Handle click to view medication details
            Intent intent = new Intent(ProfileActivity.this, MedicineRecordActivity.class);
            intent.putExtra("recordType", "Medication");
            startActivity(intent);
        });

    }

    // 从数据库获取用户数据并更新界面
    private void loadUserData(String username) {
        Database db = new Database(getApplicationContext(), "healthcare", null, 3);
        ArrayList<String> userData = db.getUserData(username);

        if (!userData.isEmpty()) {
            textViewUsername.setText(userData.get(0)); // username
            textViewPoints.setText("Points: " + userData.get(2)); // points
        } else {
            Toast.makeText(this, "未能加载用户数据", Toast.LENGTH_SHORT).show();
        }
    }
}
